//
//  TravelNotesTableViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "TravelNotesTableViewController.h"
#import "NotesTableViewCell.h"
#import "NotesModel.h"
#import "NotesLiveCollectionViewCell.h"
#import "NotesDetailViewController.h"

@interface TravelNotesTableViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *notesHomePageArray;
@property (nonatomic, strong) NSMutableArray *collectionArray;
@property (nonatomic, strong) NSString *cellIdentifier;

@end

@implementation TravelNotesTableViewController


#pragma mark - 加载数据 -

- (void)reloadView
{
    [LORequestManger GET:kNotesLive success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        for (NSDictionary *broadcastsDic in dic[@"broadcasts"]) {
            
            NotesModel *notesModel = [[NotesModel alloc] init];
            NSDictionary *logDic = broadcastsDic[@"data"][@"log"];
            [notesModel shareJsonWithDictionary:logDic];
            NSDictionary *created_byDic = logDic[@"created_by"];
            [notesModel shareJsonWithDictionary:created_byDic];
            
            if (logDic[@"posts"] && [logDic[@"posts"] count] != 0) {
                notesModel.postsArray = [[NSMutableArray alloc] init];
                for (NSDictionary *postsDic in logDic[@"posts"]) {
                    [notesModel.postsArray addObject:postsDic];
                }
            }
            
            notesModel.log_created_date = logDic[@"created_date"];
            notesModel.log_id = logDic[@"id"];
            [self.notesHomePageArray addObject:notesModel];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 加载数据
    [self reloadView];
    
    self.tableView.backgroundColor = kColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[NotesTableViewCell class] forCellReuseIdentifier:@"notesTableViewCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.notesHomePageArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   //NotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"notesTableViewCell" forIndexPath:indexPath];
    
    NotesTableViewCell *cell = [[NotesTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:nil];
    NotesModel *notesModel = self.notesHomePageArray[indexPath.row];
    [cell setFrameWithModel:notesModel];
    [cell setTarget:self moreButtonAction:@selector(morePictures:) nameButtonAction:@selector(nameButtonAction:) nicknameButtonAction:@selector(nicknameButtonAction:) index:indexPath.row];
    [cell setPraiseAndCommentWithTarget:self tapAction:@selector(tapAction:) praiseAction:@selector(praiseAction:) commentAction:@selector(commentAction:) index:indexPath.row];
    
    self.cellIdentifier = @"notesLiveCollectionViewCell";
    [cell.picturesShowCollectionView registerClass:[NotesLiveCollectionViewCell class] forCellWithReuseIdentifier:self.cellIdentifier];
    self.collectionArray = [NSMutableArray arrayWithArray:notesModel.postsArray];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesModel *notesModel = self.notesHomePageArray[indexPath.row];
    return [NotesTableViewCell setCellHeightWithModel:notesModel];
}


#pragma mark - collectView -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.collectionArray.count > 9) {
        return 9;
    }
    return self.collectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NotesLiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellIdentifier forIndexPath:indexPath];
    NotesModel *notesModel = [[NotesModel alloc] init];
    NSDictionary *dic = self.collectionArray[indexPath.row];
    [cell setModel:notesModel dictionary:dic];
    [cell setValueWithModel:notesModel];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.collectionArray.count == 1) {
        return CGSizeMake(kWidth - 20, kWidth - 20);
    }
    return CGSizeMake((kWidth - 20 - 20) / 3, (kWidth - 20 - 20) / 3);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NotesDetailViewController *detailVC = [[NotesDetailViewController alloc] init];
    NotesModel *notesModel = self.notesHomePageArray[collectionView.tag - 100000];
    detailVC.log_id = [notesModel log_id];
    NSString *pictureStr = notesModel.postsArray[indexPath.row][@"pictures"][0][@"picture"];
    detailVC.pictureStr = pictureStr;
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark - 懒加载 -

- (NSMutableArray *)notesHomePageArray
{
    if (!_notesHomePageArray) {
        _notesHomePageArray = [[NSMutableArray alloc] init];
    }
    return _notesHomePageArray;
}


#pragma mark - button点击事件 -

// 更多图片
- (void)morePictures:(UIButton *)button
{
    [self getLogidWithiIndex:button.tag - 1000 button:button];

}

// 标题进入
- (void)nameButtonAction:(UIButton *)button
{
    [self getLogidWithiIndex:button.tag - 10000 button:button];
}

- (void)nicknameButtonAction:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"个人信息");
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"%ld", tap.view.tag);
    NSLog(@"评论页面");
}

- (void)praiseAction:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"赞");
    button.selected = !button.selected;
    if (button.selected) {
        [button setBackgroundImage:[UIImage imageNamed:@"icon_greated_32x32@2x"] forState:UIControlStateSelected];
    } else {
        [button setBackgroundImage:[UIImage imageNamed:@"icon_great_gray_13x13@3x"] forState:UIControlStateNormal];
    }
}

- (void)commentAction:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    NSLog(@"评论页面");
}

- (void)getLogidWithiIndex:(NSInteger)index button:(UIButton *)button
{
    NotesDetailViewController *detailVC = [[NotesDetailViewController alloc] init];
    detailVC.log_id = [self.notesHomePageArray[index] log_id];
    NSString *title = [button titleForState:UIControlStateNormal];
    
    if ([title isEqualToString:@"更多图片..."]) {
        
        NotesModel *model = self.notesHomePageArray[index];
        NSString *pictureStr = model.postsArray[8][@"pictures"][0][@"picture"];
        detailVC.pictureStr = pictureStr;
        
    }
    [self.navigationController setAnimationWithSubtype:0 andAnimationType:8];
    [self.navigationController pushViewController:detailVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
